﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using DTOFrameworks;
using System.ComponentModel;
using FooFun.DataElements.Dtos;

namespace FooFun.Web.Controls
{
    [ParseChildren(true), PersistChildren(false)]
    [Obsolete("使用新的TreeElement代替")]
    class HierarchicalElement : Control, INamingContainer
    {
        ITreeVisitor<IDataElement> visitor;

        public HierarchicalElement()
        {
            this.ChildControlsCreated = false;
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            ListChild(string.Empty);

            this.ChildControlsCreated = true;
        }

        IDataElement de;
        DataElementTemplateControl ctr;

        private void ListChild(string level)
        {
            if (visitor.MoveFirstChild())
            {
                if (MoveChildTemplate != null)
                {
                    ctr = new DataElementTemplateControl();
                    ctr.Level = level;

                    MoveChildTemplate.InstantiateIn(ctr);
                    this.Controls.Add(ctr);

                    ctr.DataBind();
                }

                if (ItemTemplate != null)
                {
                    de = (IDataElement)visitor.GetValue();

                    ctr = new DataElementTemplateControl();

                    ctr.Level = level;

                    ItemTemplate.InstantiateIn(ctr);

                    this.Controls.Add(ctr);

                    ctr.DataElement = de;
                    ctr.DataBind();

                }

                ListChild(level + this.Level);

                while (visitor.MoveNext())
                {
                    if (MoveNextTemplate != null)
                    {
                        ctr = new DataElementTemplateControl();
                        ctr.Level = level;
                        MoveNextTemplate.InstantiateIn(ctr);

                        this.Controls.Add(ctr);

                        ctr.DataBind();
                    }

                    if (ItemTemplate != null)
                    {
                        de = visitor.GetValue();
                        ctr = new DataElementTemplateControl();


                        ItemTemplate.InstantiateIn(ctr);
                        this.Controls.Add(ctr);

                        ctr.DataElement = de;
                        ctr.Level = level;

                        ctr.DataBind();


                    }
                    ListChild(level + this.Level);
                }

                if (MoveUpTemplate != null)
                {
                    ctr = new DataElementTemplateControl();
                    MoveUpTemplate.InstantiateIn(ctr);
                    this.Controls.Add(ctr);

                    ctr.Level = level;

                    ctr.DataBind();


                }

                visitor.MoveUp();
            }
        }

        public string Level { get; set; }

        public object Visitor
        {
            get
            {
                return visitor;
            }
            set
            {
                if (value is ITreeVisitor<IDataElement>)
                {
                    visitor = (ITreeVisitor<IDataElement>)value;
                }
            }
        }

        [
           Browsable(false),
           PersistenceMode(PersistenceMode.InnerProperty),
           Description("移动到首个子数据"),
           TemplateContainer(typeof(DataElementTemplateControl))
        ]
        public ITemplate MoveChildTemplate { get; set; }

        [
           Browsable(false),
           PersistenceMode(PersistenceMode.InnerProperty),
           Description("普通项模板"),
           TemplateContainer(typeof(DataElementTemplateControl))
        ]
        public ITemplate ItemTemplate { get; set; }

        [
           Browsable(false),
           PersistenceMode(PersistenceMode.InnerProperty),
           Description("移动到下一个数据项"),
           TemplateContainer(typeof(DataElementTemplateControl))
        ]
        public ITemplate MoveNextTemplate { get; set; }

        [
           Browsable(false),
           PersistenceMode(PersistenceMode.InnerProperty),
           Description("移动到上一数据项"),
           TemplateContainer(typeof(DataElementTemplateControl))
        ]
        public ITemplate MoveUpTemplate { get; set; }
    }

    [ParseChildren(true), PersistChildren(false)]
    public class TreeElement : Control, IDataElementBinder
    {
        public TreeElement()
        {
            this.ChildControlsCreated = false;
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            ListChild(dataElement, string.Empty);

            this.ChildControlsCreated = true;
        }

        DataElementTemplateControl ctr;
        private void ListChild(IDataElement currentDE, string level)
        {
            if (!currentDE.IsEmpty)
            {
                if (HeaderTemplate != null)
                {
                    ctr = new DataElementTemplateControl();
                    ctr.DataElement = currentDE;

                    HeaderTemplate.InstantiateIn(ctr);

                    this.Controls.Add(ctr);
                    ctr.DataBind();
                }

                foreach (IDataElement childDE in currentDE)
                {
                    if (ItemHeaderTemplate != null)
                    {
                        ctr = new DataElementTemplateControl();
                        ctr.DataElement = childDE;

                        ItemHeaderTemplate.InstantiateIn(ctr);

                        this.Controls.Add(ctr);
                        ctr.DataBind();
                    }

                    ctr = new DataElementTemplateControl();
                    ctr.DataElement = childDE;
                    ctr.Level = level;

                    ItemTemplate.InstantiateIn(ctr);

                    this.Controls.Add(ctr);
                    ctr.DataBind();

                    ListChild(childDE, level + this.Level);

                    if (ItemFooterTemplate != null)
                    {

                        ctr = new DataElementTemplateControl();
                        ctr.DataElement = childDE;

                        ItemFooterTemplate.InstantiateIn(ctr);

                        this.Controls.Add(ctr);
                        ctr.DataBind();
                    }
                }

                if (FooterTemplate != null)
                {
                    ctr = new DataElementTemplateControl();
                    ctr.DataElement = currentDE;

                    FooterTemplate.InstantiateIn(ctr);

                    this.Controls.Add(ctr);
                    ctr.DataBind();
                }
            }
        }

        public string Level { get; set; }

        [
           Browsable(false),
           PersistenceMode(PersistenceMode.InnerProperty),
           Description("循环数据项前的头模板"),
           TemplateContainer(typeof(DataElementTemplateControl))
        ]
        public ITemplate HeaderTemplate { get; set; }

        [
           Browsable(false),
           PersistenceMode(PersistenceMode.InnerProperty),
           Description("循环数据项后的脚模板"),
           TemplateContainer(typeof(DataElementTemplateControl))
        ]
        public ITemplate FooterTemplate { get; set; }

        [
           Browsable(false),
           PersistenceMode(PersistenceMode.InnerProperty),
           Description("普通项模板"),
           TemplateContainer(typeof(DataElementTemplateControl))
        ]

        public ITemplate ItemTemplate { get; set; }

        [
           Browsable(false),
           PersistenceMode(PersistenceMode.InnerProperty),
           Description("数据项前的头模板"),
           TemplateContainer(typeof(DataElementTemplateControl))
        ]
        public ITemplate ItemHeaderTemplate { get; set; }

        [
           Browsable(false),
           PersistenceMode(PersistenceMode.InnerProperty),
           Description("数据项后的脚模板"),
           TemplateContainer(typeof(DataElementTemplateControl))
        ]
        public ITemplate ItemFooterTemplate { get; set; }

        #region IDataElementBinder 成员

        IDataElement dataElement;
        public IDataElement DataElement
        {
            get
            {
                return dataElement;
            }
            set
            {
                dataElement = value;
            }
        }

        #endregion
    }

    public class DataElementTemplateControl
           : Control, IDataElementBinder, INamingContainer
    {
        public IDataElement DataElement { get; set; }

        private String level;

        public String Level
        {
            get { return level; }
            set { level = value; }
        }
    }

}